package com.hkbigdata.sybx.controller;

/**
 * @author yangjing
 * @creat 2025-05-28-9:02
 * @email 3010335359@qq.com
 */

import com.hkbigdata.sybx.bean.UserLoginRecord;
import com.hkbigdata.sybx.service.UserLoginRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/userLoginRecord")
public class UserLoginRecordController {

    @Autowired
    private UserLoginRecordService userLoginRecordService;

    /**
     * 插入登录记录
     */
    @PostMapping("/insert")
    public ResponseEntity<Map<String, Object>> insertLoginRecord(@RequestBody UserLoginRecord userLoginRecord) {
        Map<String, Object> result = new HashMap<>();
        try {
            System.out.println("[Controller] 接收到登录记录信息: " + userLoginRecord);
            String insertResult = userLoginRecordService.insertLoginRecord(userLoginRecord);

            if ("success".equals(insertResult)) {
                result.put("code", 200);
                result.put("message", "插入成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("code", 400);
                result.put("message", insertResult);
                return ResponseEntity.badRequest().body(result);
            }
        } catch (IllegalArgumentException e) {
            result.put("code", 400);
            result.put("message", "参数错误: " + e.getMessage());
            return ResponseEntity.badRequest().body(result);
        } catch (Exception e) {
            System.err.println("[Controller] 插入失败: " + e.getMessage());
            e.printStackTrace();
            result.put("code", 500);
            result.put("message", "服务器内部错误，请联系管理员");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据用户ID查询登录记录
     */
    @GetMapping("/records/{phone_number}")
    public ResponseEntity<Map<String, Object>> getLoginRecordsByUserId(@PathVariable Long phone_number) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<UserLoginRecord> records = userLoginRecordService.getLoginRecordsByUserId(phone_number);
            if (records != null && !records.isEmpty()) {
                result.put("code", 200);
                result.put("message", "查询成功");
                result.put("data", records);
                return ResponseEntity.ok(result);
            } else {
                result.put("code", 404);
                result.put("message", "未找到该用户的登录记录");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}